package zm.irc.threads;

import org.apache.log4j.Logger;

import zm.irc.client.IrcClient;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.receive.IrcReceiveMessage;
import zm.irc.msgqueue.LocalMemoryMsgQueue;


/**
 * <pre>
 * Collect irc message from IRC client( Inputstream )
 * Add the message into {@link LocalMemoryMsgQueue}
 * 
 * All message will be processed by {@link RecvMsgProcessThread}
 * </pre>
 */
@Service
public class RecvMsgCollectThread extends IrcThread{
    private static Logger log = Logger.getLogger(RecvMsgCollectThread.class);

    @AutoWired
    private LocalMemoryMsgQueue localMemoryMsgQueue ;

    @AutoWired
    private IrcClient ircClient;

    
    private String msg;
    
    
    @Override
    public void beforeLoop(){
        log.info("Recv Collector Thread Start.");
    }

    @Override
    public void before() throws Exception{
        msg = this.ircClient.readLine();
    }

    @Override
    public void doRun() {
        try {
            if (msg == null) {
                return;
            }
            IrcReceiveMessage receivedMsg = IrcReceiveMessage.build(msg, this.ircClient);

            String channelName = receivedMsg.getChannel();
            this.localMemoryMsgQueue.receiveMsg(channelName, receivedMsg);

        } catch (Exception e) {
            log.error("error", e);
        }
    }



}
